import 'package:common_ui/common_ui.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:manager/configs/theme_data.dart';
import 'package:manager/res/color.dart';

import '../models/interfacer/title_parent.dart';


///显示列表类型bottomsheet
///[dataList]传入的数据必须继承TitleParent 类,以暴露获取title方法
///[height]如果为null,则会根据数据列表的长度进行计算,最大高度为屏幕高度*0.6
void showListBottomSheet<T extends TitleParent>(
    List<T> dataList, Function(T) itemClick,
    {String? title, double? height, bool? showCancelButton}) {
  Get.bottomSheet(Container(
    height: height ??
        ((showCancelButton ?? true) ? (dataList.length + 1) : dataList.length) *
                50 +
            34,
    constraints: BoxConstraints(minHeight: 100, maxHeight: Get.height * 0.6),
    decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(8))),
    child: Column(
      children: [
        SingleText(
          title ?? '',
          textStyle: Styles.textSmall,
          alignment: Alignment.center,
          padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
          bgColor: ColorPool.activity_bg,
          borderRadius: const BorderRadius.vertical(top: Radius.circular(8)),
        ),
        Expanded(
            child: SingleChildScrollView(
          child: Column(
            children: dataList
                .map((e) => InkWell(
                      onTap: () {
                        itemClick(e);
                        Get.back(closeOverlays: false);
                      },
                      child: SingleText(
                        e.getTitle(),
                        height: 50,
                        alignment: Alignment.center,
                        border: const Border(
                            bottom: BorderSide(color: ColorPool.line_color)),
                      ),
                    ))
                .toList(),
          ),
        )),
        (showCancelButton ?? true)
            ? Container(
                width: double.infinity,
                height: 50,
                color: Colors.grey[100],
                child: TextButton(
                    onPressed: () {
                      Get.back(closeOverlays: false);
                    },
                    child: const Text(
                      '取消',
                      style:
                          TextStyle(color: ColorPool.theme_color, fontSize: 16),
                    )),
              )
            : Container()
      ],
    ),
  ));
}

